package com.imooc;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @program: scala-JDBCtest
 * @description: JDBCtest
 * @author: zengchen
 * @create: 2021-04-22 16:21
 **/
public class JDBCtest {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        String url = "jdbc:mysql://centos704:3306/flume";
        String user = "root";
        String pass = "123456";
        String sql = "select id,name,age,city from user";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(url, user, pass);
            Statement stmt = conn.createStatement();
            boolean hasResultSet = stmt.execute(sql);
            if (hasResultSet) {
                ResultSet rs = stmt.getResultSet();
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    System.out.println(id + "," + name);
                }
            }
        } catch (ClassNotFoundException e1) {

        } catch (SQLException e2) {

        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }
}
